<?php  
	namespace Admin\Controller;
	class BranchController extends CommonController
	{
		public function index()
		{
			$list = M('branch') -> select();
			//$list2 = M('branch_and_creategory') -> select();
			$this -> assign('list',$list);
			//$this -> assign('list2',$list2);
			$this -> display();
		}

		public function Add()
		{
			$branck_name = trim(I('post.branck_name'));
			if (!empty($branck_name)) 
			{
				$Model = new \Think\Model();
				$num = $Model->query("select count(*) as num from y_branch where brank_name=\"$branck_name\"");
				$sel = $num['0']['num'];
				if ($sel === '0') 
				{
					$data = [
							'brank_name' => $branck_name,
							'create_time' => time(),
							'update_time' => time(),
							'change_id' => $_SESSION['admin_info']['aid']
							];
					M('branch') -> data($data) -> add();
				}
			}
			$this -> redirect('Branch/index');
		}

		public function Edit()
		{
			$id = trim(I('post.id'));
			$brank_name = trim(I('post.branck_name'));
			if (!empty($brank_name)) 
			{
				$data = [
						'brank_name' => $brank_name,
						'update_time' => time(),
						'change_id' => $_SESSION['admin_info']['aid']
						];
				$where['id'] = $id;
				M('branch') -> where($where) -> save($data);
			}
			$this -> redirect('Branch/index');
		}

		public function Del()
		{
			$id = trim(I('post.id'));
			$Model = new \Think\Model();
			$num = $Model->execute("delete from y_branch where id=\"$id\"");
			$this -> redirect('Branch/index');
		}

		public function AddCategory()
		{
			$branch_id = trim(I('branch_id'));
			$category_name = trim(I('category_name'));
			$min_jifen = trim(I('min_jifen'));
			$mix_jifen = trim(I('mix_jifen'));
			if($min_jifen>$mix_jifen) return $this->error('积分区间错误');
			$number_add = trim(I('opt'));
				if (!empty($category_name) && $number_add!='') 
			{
				$Model = new \Think\Model();
				$num = $Model->query("select count(*) as num from y_category where branch_id=\"$branch_id\" and category_name=\"$category_name\" ");
				$sel = $num['0']['num'];
				if ($sel === '0') 
				{
					$data = [
							'branch_id' => $branch_id,
							'category_name' => $category_name,
							'add_number' => 0,
							'min_jifen'=>$min_jifen,
							'max_jifen'=>$mix_jifen,
							'opt' => $number_add,
							'create_time' => time(),
							'update_time' => time(),
							'change_id' => $_SESSION['admin_info']['aid']
							];
					M('category') -> data($data) -> add();
				}
				
			}

			$this -> redirect('Integral/index');
		}

		public function EditCategory()
		{
			$cid = trim(I('cid'));
			$branch_id =  trim(I('post.branch_id'));
			$category_name = trim(I('post.category_name'));
			$number_add = trim(I('post.number_add'));
			$add_number = trim(I('post.add_number'));

			if (!empty($branch_id) && !empty($category_name) && $number_add != '' || $number_add == null && !empty($add_number)) 
			{
				$data = [
						'category_name' => $category_name,
						'add_number' => $add_number,
						'opt' => $number_add,
						'update_time' => time(),
						'change_id' => $_SESSION['admin_info']['aid']
						];
				$where['id'] = $cid;
				$where['branch_id'] = $branch_id;
				M('category') -> where($where) -> save($data);
			}
			$this -> redirect('Integral/index');
		}

		public function DelCategory()
		{
			$cid = trim(I('cid'));
			$Model = new \Think\Model();
			$num = $Model->execute("delete from y_category where id=\"$cid\"");
			$this -> redirect('Integral/index');
		}
	}